#include "book_info_manage.h"
#include "ui_book_info_manage.h"


book_info_manage::book_info_manage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::book_info_manage)
{
    ui->setupUi(this);
    setWindowTitle("书籍信息资料管理");
    setWinStyle();
    bookInfoTableOP();
}

book_info_manage::~book_info_manage()
{
    delete ui;
}

void book_info_manage::setWinStyle()
{
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->bookCode_lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                         "color: rgb(31,31,31);"		//字体颜色
                                         "padding-left:20px;"       //内边距-字体缩进
                                         "background-color: rgb(255, 255, 255);" //背景颜色
                                         "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->bookname_lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                         "color: rgb(31,31,31);"		//字体颜色
                                         "padding-left:20px;"       //内边距-字体缩进
                                         "background-color: rgb(255, 255, 255);" //背景颜色
                                         "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->category_lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                         "color: rgb(31,31,31);"		//字体颜色
                                         "padding-left:20px;"       //内边距-字体缩进
                                         "background-color: rgb(255, 255, 255);" //背景颜色
                                         "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->tableView->setAlternatingRowColors(true); // 表格数据行隔行变色
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection); // 单个数据格
    ui->add_pushButton->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:36px '宋体';}"
                "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
                "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");

    ui->delete_pushButton->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:36px '宋体';}"
                "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
                "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");

    ui->search_pushButton->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:36px '宋体';}"
                "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
                "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");
}

void book_info_manage::bookInfoTableOP()
{
    M = new QSqlTableModel;
    M->setTable("book_info");
    M->select();
    ui->tableView->setModel(M);
    //M->setEditStrategy(QSqlTableModel::OnManualSubmit); // 表格数据更新数据库，要手工提交
    //    ui->tableView->setColumnHidden(0, true);
    //    ui->tableView->setColumnHidden(2, true);
    QString headStr[] = {"图书编号", "图书名称", "图书类型", "作者", "价格", "语言", "出版日期","库存数量"};
    int colWidths[] = {100, 100, 100, 100, 60, 60, 288,50};

    for(int i=0; i<8; i++)
    {
        M->setHeaderData(i, Qt::Horizontal, headStr[i]); // 设置表格 column 列名称
        ui->tableView->setColumnWidth(i, colWidths[i]); // 设置表格 column 列宽度
    }
    //ui->tableView->verticalHeader()->setVisible(false); // 隐藏 第一列 名称
    ui->tableView->horizontalHeader()->setStyleSheet("QHeaderView::section {"
                                                     "color: brown;padding-left: 1px;border: 1px solid #6c6c6c;}");
    // 设置表格 column 列的样式
    ui->tableView->horizontalHeader()->setMinimumHeight(30); // 设置表头高度
}





void book_info_manage::on_delete_pushButton_clicked()
{
    QMessageBox msgBox;
    msgBox.setWindowTitle("书籍管理");
    msgBox.setText("提示");
    msgBox.setInformativeText("确实要删除吗?");
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Ok);
    int ret = msgBox.exec();
    if(ret == QMessageBox::Ok){
        QModelIndex curIndex = ui->tableView->currentIndex();
        M->removeRow(curIndex.row());
        M->select();
    }
}



void book_info_manage::on_search_pushButton_clicked(){
    QString bookCode = ui->bookCode_lineEdit->text();
    QString bookName=ui->bookname_lineEdit->text();
    QString category = ui->category_lineEdit->text();
    QString sql;
    if (bookCode.isEmpty() && bookName.isEmpty()&&category.isEmpty()) {
        sql = "";
    }
    else if(bookCode.isEmpty()&&bookName.isEmpty()){
        sql = QString("category like '%%1%'").arg(category);
    }
    else if(bookCode.isEmpty()&&category.isEmpty()){
        sql = QString("bookName like '%%1%'").arg(bookName);
    }
    else if(category.isEmpty()&&bookName.isEmpty()){
        sql = QString("bookCode like '%%1%'").arg(bookCode);
    }
    else if (bookCode.isEmpty()) {
        sql = QString("(bookName like '%%1%') AND (category like '%%2%')").arg(bookName, category);
    }
    else if (bookName.isEmpty()) {
        sql = QString("(bookCode like '%%1%') AND (category like '%%2%')").arg(bookCode, category);
    }
    else if(category.isEmpty()){
        sql = QString("(bookCode like '%%1%') AND (bookName like '%%2%')").arg(bookCode, bookName);
    }
    else {
        sql = QString("(bookCode like '%%1%') AND (bookName like '%%2%') AND (category like '%%3%')").arg(bookCode, bookName,category);
    }
    M->setFilter(sql);
}


void book_info_manage::on_add_pushButton_clicked()
{
    bookInfo_add = new book_Info_add(M, this);
    bookInfo_add->show();
    M->select();
}

